using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;

using MaskData;

namespace DBProxy
{
	internal class AccessMaskNodeWriter : IMaskNodeWriter
	{
		private DBConnectionService _DBService = DBConnectionService.Instance();
		public void SaveMaskNode(MaskNode node, String maskNodeId, String fieldName, String fieldValue)
		{
			String tableName = _DBService.GetTableNameByID(maskNodeId);

			String sqlString = "select * from "
				+ tableName
				+ " where "
				+ fieldName
				+ " = '"
				+ fieldValue
				+ "'";

			OleDbDataAdapter dAdapter = _DBService.GetAdapter(sqlString);
			OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dAdapter);
			commandBuilder.QuotePrefix = "[";
			commandBuilder.QuoteSuffix = "]";

			DataSet nodeSet = new DataSet();
			dAdapter.Fill(nodeSet, tableName);
			DataRowCollection rows = nodeSet.Tables[tableName].Rows;
			Debug.Assert(rows.Count == 1);

			node.SaveToRow(rows[0]);

			dAdapter.Update(nodeSet.Tables[tableName]);
		}
	}
}
